package com.ktwlrj.core.config;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * 缓存配置
 *
 * @author zhouxuanhong
 * @date 2019-06-05 10:10
 * @since JDK1.8
 */
@Configuration
@EnableCaching
public class CacheConfig {

    /**
     * Caffeine配置
     *
     * @return CacheManager ${@link CacheManager}
     * @author zhouxuanhong
     * @date 2019-06-05 10:13
     */
    @Bean
    @Qualifier
    public CacheManager caffeineCacheManager() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        Caffeine<Object, Object> caffeine = Caffeine.newBuilder()
                // 初始容量
                .initialCapacity(100)
                // 最大容量
                .maximumSize(1000)
                // 过期时间
                .expireAfterWrite(300, TimeUnit.SECONDS)
                // 刷新时间
                .expireAfterAccess(120, TimeUnit.SECONDS);
        cacheManager.setCaffeine(caffeine);
        return cacheManager;
    }
}

